//package com.ethjava.utils;
//
//import com.ethjava.utils.Environment;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.web3j.abi.FunctionEncoder;
//import org.web3j.abi.TypeReference;
//import org.web3j.abi.datatypes.Address;
//import org.web3j.abi.datatypes.Bool;
//import org.web3j.abi.datatypes.DynamicArray;
//import org.web3j.abi.datatypes.Function;
//import org.web3j.abi.datatypes.Int;
//import org.web3j.abi.datatypes.Type;
//import org.web3j.abi.datatypes.generated.Uint256;
//import org.web3j.crypto.*;
//import org.web3j.protocol.ObjectMapperFactory;
//import org.web3j.protocol.Web3j;
//import org.web3j.protocol.core.DefaultBlockParameterName;
//import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
//import org.web3j.protocol.core.methods.response.EthSendTransaction;
//import org.web3j.protocol.http.HttpService;
//import org.web3j.tx.ChainId;
//import org.web3j.tx.Contract;
//import org.web3j.utils.Convert;
//import org.web3j.utils.Numeric;
//
//import java.io.IOException;
//import java.math.BigDecimal;
//import java.math.BigInteger;
//import java.security.InvalidAlgorithmParameterException;
//import java.security.NoSuchAlgorithmException;
//import java.security.NoSuchProviderException;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * ��Ǯ��
// * �˺� �������
// */
//public class BscColdWallet {
//
//	private static Web3j web3j;
//
//	private static String d = "/Users/yangzhengwei/Documents/eth/coldwallet";
//
//	private static String address = "";
//
//	private static String keystore = "{\"address\":\"a530d89646db11abfa701e148e87324355fc6ea7\",\"id\":\"246e7d1d-8f31-4a3e-951d-41722213a44f\",\"version\":3,\"crypto\":{\"cipher\":\"aes-128-ctr\",\"ciphertext\":\"26d10977bc199f6b678e89d3b7c3874bab3cddda18b92c014890d80657d7cc6a\",\"cipherparams\":{\"iv\":\"beaa9a404f793e86460a1fc71a0372a8\"},\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":262144,\"p\":1,\"r\":8,\"salt\":\"f06eb3d208db1643671c6e0210789f05e6de1746252fe5b83a38618e2bd18f1e\"},\"mac\":\"0aa4f85dfecaf8203ad0ee22c47ff6fb35b8f47d8f56ddb890ef2d513a06a801\"}}\n";
//
//	private static String privateKey = "";
//
//	private static String bscContract = "0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F";
//
//	private static String tkUsdtAddr = "55d398326f99059fF775485246999027B3197955";
//
//	private static String tkBusdAddr = "e9e7CEA3DedcA5984780Bafc599bD69ADd087D56";
//
//	private static String tkASWAddr = "0x0Eb3a705fc54725037CC9e008bDede697f62F335";
//
//	static double bnbValue = 512d;
//	static double tokenValue = 5d;
//
//
//
//	public static void main(String[] args) {
//		web3j = Web3j.build(new HttpService(Environment.BSC_MAIN_RPC_URL));
//		try {
////			createWallet("11111111");
////			decryptWallet(keystore, "11111111");
////			testTransaction();
////			swap(web3j,address,privateKey
////					,"0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F",0.5d,18);
////			approve(web3j,"0x4aF36Ce12660d9F34d9656Bc11C73C293C4BFCAC",
////					"01f270f673eadbbb7e99e3d64858fe00b5555110449ef02e0ff8100d8b6884af",
////					"0x52d86850bc8207b520340b7e39cdaf22561b9e56",  //approved ��Լ��ַ
////					"05fF2B0DB69458A0750badebc4f9e13aDd608C7F",3d,18);
////			for(int i =1;i<=40; i++){
//				swap(web3j,"0x4aF36Ce12660d9F34d9656Bc11C73C293C4BFCAC",
//						"01f270f673eadbbb7e99e3d64858fe00b5555110449ef02e0ff8100d8b6884af"
//						,"0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F",
//						"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
//						"0x32ee6823eefedfbde2b9a3b299b835e703a09562",
//						1d,18);
//
////			swap2(web3j,address,privateKey,
////					bscContract,tkBusdAddr,"0x4e1b16ef22935a575a6811d4616f98c4077e4408",
////					1d,18);
////
////				Thread.sleep(5000);
////			}
//
//
//
////			swap2(web3j,address,privateKey,
////					bscContract,tkBusdAddr,"0x000006d127f85efcd5358484b87183705333ed91",
////					1000d,18);
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//
//	//���ͽ���
//	private static void testTransaction() {
//		BigInteger nonce;
//		EthGetTransactionCount ethGetTransactionCount = null;
//		try {
//			ethGetTransactionCount = web3j.ethGetTransactionCount(address, DefaultBlockParameterName.PENDING).send();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		if (ethGetTransactionCount == null) return;
//		nonce = ethGetTransactionCount.getTransactionCount();
//		BigInteger gasPrice = Convert.toWei(BigDecimal.valueOf(5), Convert.Unit.GWEI).toBigInteger();
//		BigInteger gasLimit = BigInteger.valueOf(30000);
//		String to = "0x4aF36Ce12660d9F34d9656Bc11C73C293C4BFCAC".toLowerCase();
//		BigInteger value = Convert.toWei(BigDecimal.valueOf(0.5), Convert.Unit.ETHER).toBigInteger();
//		String data = "";
////		byte chainId = ChainId.ROPSTEN;//��������
//		byte chainId = ChainId.NONE;
//		String privateKey = BscColdWallet.privateKey;
//		String signedData;
//		try {
//			signedData = signTransaction(nonce, gasPrice, gasLimit, to, value, data, chainId, privateKey);
//			if (signedData != null) {
//				EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(signedData).send();
//				System.out.println(ethSendTransaction.getTransactionHash());
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
//
//	private static void approve(Web3j web3j, String fromAddress, String privateKey, String to,
//			String contractAddress, double amount, int decimals) {
//		BigInteger nonce;
//		EthGetTransactionCount ethGetTransactionCount = null;
//		try {
//			ethGetTransactionCount = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.PENDING).send();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		if (ethGetTransactionCount == null) return;
//		nonce = ethGetTransactionCount.getTransactionCount();
//		System.out.println("nonce " + nonce);
//		BigInteger gasPrice = Convert.toWei(BigDecimal.valueOf(5), Convert.Unit.GWEI).toBigInteger();
//		BigInteger gasLimit = BigInteger.valueOf(60000);
//		BigInteger value = BigInteger.ZERO;
//		//tokenת�˲���
//		String methodName = "approve";
//		List<Type> inputParameters = new ArrayList<>();
//		List<TypeReference<?>> outputParameters = new ArrayList<>();
//		Address tAddress = new Address(contractAddress);
//		Uint256 tokenValue = new Uint256(BigDecimal.valueOf(amount).multiply(BigDecimal.TEN.pow(decimals)).toBigInteger());
//		inputParameters.add(tAddress);
//		inputParameters.add(tokenValue);
//		TypeReference<Bool> typeReference = new TypeReference<Bool>() {
//		};
//		outputParameters.add(typeReference);
//		Function function = new Function(methodName, inputParameters, outputParameters);
//		String data = FunctionEncoder.encode(function);
//
//		byte chainId = ChainId.NONE;
//		String signedData;
//		try {
//			signedData = BscColdWallet.signTransaction(nonce, gasPrice, gasLimit, to, value, data, chainId, privateKey);
//			if (signedData != null) {
//				EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(signedData).send();
//				System.out.println(ethSendTransaction.getTransactionHash());
//				System.out.println("result:"+ethSendTransaction.getResult());
//
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
//
//
//	public static void swap(Web3j web3j, String fromAddress, String privateKey,
//			String contractAddress,String tkAddress,String tk2Add, double amount, int decimals) {
//		BigInteger nonce;
//		EthGetTransactionCount ethGetTransactionCount = null;
//		try {
//			ethGetTransactionCount = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.PENDING).send();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		if (ethGetTransactionCount == null) return;
//		nonce = ethGetTransactionCount.getTransactionCount();
//		System.out.println("nonce " + nonce);
//		BigInteger gasPrice = Convert.toWei(BigDecimal.valueOf(10), Convert.Unit.GWEI).toBigInteger();
//		BigInteger gasLimit = BigInteger.valueOf(237306);
//		BigInteger value = Convert.toWei(BigDecimal.valueOf(amount), Convert.Unit.ETHER).toBigInteger();
//
//		//tokenת�˲���
//		String methodName = "swapExactETHForTokens";
//		List<Type> inputParameters = new ArrayList<>();
//		List<TypeReference<?>> outputParameters = new ArrayList<>();
//		Address fAddress = new Address(fromAddress);
////		Uint256 tokenValue = new Uint256(BigDecimal.valueOf(amount).multiply(BigDecimal.TEN.pow(decimals)).toBigInteger());
//		//2595586952605866924903436461827
//
//		   BigDecimal tokenValue2 = BigDecimal.valueOf(tokenValue).multiply(BigDecimal.valueOf(0.4));
//		   BigInteger valuebig =    BigDecimal.valueOf(amount).multiply(BigDecimal.valueOf(bnbValue)).divide(tokenValue2.add(BigDecimal.valueOf(tokenValue)),0,BigDecimal.ROUND_HALF_UP).toBigInteger();
//
//		Uint256 tokenValue = new Uint256(valuebig);
//		inputParameters.add(tokenValue);
//		 List<Address> arrays = new ArrayList<Address>();
//		 arrays.add(new Address(tkAddress));
//		 arrays.add(new Address(tk2Add));
//		 DynamicArray darray = new DynamicArray(arrays);
//		inputParameters.add(darray);
//		inputParameters.add(fAddress);
//		Uint256 deadline = new Uint256(System.currentTimeMillis());
//		inputParameters.add(deadline);
//		TypeReference<Bool> typeReference = new TypeReference<Bool>() {
//		};
//		outputParameters.add(typeReference);
//
//		Function function = new Function(methodName, inputParameters, outputParameters);
//		String data = FunctionEncoder.encode(function);
//
//		byte chainId = ChainId.NONE;
//		String signedData;
//		try {
//			signedData = BscColdWallet.signTransaction(nonce, gasPrice, gasLimit, contractAddress, value, data, chainId, privateKey);
//			if (signedData != null) {
//				EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(signedData).send();
////				ethSendTransaction.
////				web3j.e
////				web3j.ethSendTransaction();
//				System.out.println(ethSendTransaction.getTransactionHash());
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
//
//
//	private static void swap2(Web3j web3j, String fromAddress, String privateKey,
//			String contractAddress, String tK1Addr,String tk2Addr,double amount, int decimals) {
//		BigInteger nonce;
//		EthGetTransactionCount ethGetTransactionCount = null;
//		try {
//			ethGetTransactionCount = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.PENDING).send();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		if (ethGetTransactionCount == null) return;
//		nonce = ethGetTransactionCount.getTransactionCount();
//		System.out.println("nonce " + nonce);
//		BigInteger gasPrice = Convert.toWei(BigDecimal.valueOf(5), Convert.Unit.GWEI).toBigInteger();
//		BigInteger gasLimit = BigInteger.valueOf(129104);
//		BigInteger value = Convert.toWei(BigDecimal.valueOf(amount), Convert.Unit.ETHER).toBigInteger();
//		BigInteger value_outmin = Convert.toWei(BigDecimal.valueOf(0.00001), Convert.Unit.ETHER).toBigInteger();
//		//tokenת�˲���		 SWAPEXACTTOKENSFOR TOKENS
//		String methodName = "swapExactTokensForTokens";
//		List<Type> inputParameters = new ArrayList<>();
//		List<TypeReference<?>> outputParameters = new ArrayList<>();
//		inputParameters.add( new Uint256(value));
//		inputParameters.add( new Uint256(value_outmin));
//		 List<Address> arrays = new ArrayList<Address>();
//		 arrays.add(new Address(tK1Addr));
////		 arrays.add(new Address("0x2170ed0880ac9a755fd29b2688956bd959f933f8")); // Binance-Peg Ethereum Token (ETH)
////		 arrays.add(new Address("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c")); //BNB
//		 arrays.add(new Address(tk2Addr));
//		 DynamicArray darray = new DynamicArray(arrays);
//		inputParameters.add(darray);
//		inputParameters.add( new Address(fromAddress));
//		Uint256 deadline = new Uint256(System.currentTimeMillis());
//		inputParameters.add(deadline);
//		TypeReference<Bool> typeReference = new TypeReference<Bool>() {
//		};
//		outputParameters.add(typeReference);
//
//		Function function = new Function(methodName, inputParameters, outputParameters);
//		String data = FunctionEncoder.encode(function);
//
//		byte chainId = ChainId.NONE;
//		String signedData;
//		try {
//			signedData = BscColdWallet.signTransaction(nonce, gasPrice, gasLimit, contractAddress, BigInteger.ZERO, data, chainId, privateKey);
//			if (signedData != null) {
//				EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(signedData).send();
//				System.out.println(ethSendTransaction.getTransactionHash());
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
//
//
//	/**
//	 * ����Ǯ��
//	 *
//	 * @param password ����
//	 */
//	public static void createWallet(String password) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, CipherException, JsonProcessingException {
//		WalletFile walletFile;
//		ECKeyPair ecKeyPair = Keys.createEcKeyPair();
//		walletFile = Wallet.createStandard(password, ecKeyPair);
//		System.out.println("address " + walletFile.getAddress());
//		ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
//		String jsonStr = objectMapper.writeValueAsString(walletFile);
//		System.out.println("keystore json file " + jsonStr);
//	}
//
//	/**
//	 * ����keystore �õ�˽Կ
//	 *
//	 * @param keystore
//	 * @param password
//	 */
//	public static String decryptWallet(String keystore, String password) {
//		String privateKey = null;
//		ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
//		try {
//			WalletFile walletFile = objectMapper.readValue(keystore, WalletFile.class);
//			ECKeyPair ecKeyPair = null;
//			ecKeyPair = Wallet.decrypt(password, walletFile);
//			privateKey = ecKeyPair.getPrivateKey().toString(16);
//			System.out.println(privateKey);
//		} catch (CipherException e) {
//			if ("Invalid password provided".equals(e.getMessage())) {
//				System.out.println("�������");
//			}
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return privateKey;
//	}
//
//	/**
//	 * ǩ������
//	 */
//	public static String signTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to,
//										 BigInteger value, String data, byte chainId, String privateKey) throws IOException {
//		byte[] signedMessage;
//		RawTransaction rawTransaction = RawTransaction.createTransaction(
//				nonce,
//				gasPrice,
//				gasLimit,
//				to,
//				value,
//				data);
//
//		if (privateKey.startsWith("0x")) {
//			privateKey = privateKey.substring(2);
//		}
//		ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
//		Credentials credentials = Credentials.create(ecKeyPair);
//
//		if (chainId > ChainId.NONE) {
//			signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
//		} else {
//			signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
//		}
//
//		String hexValue = Numeric.toHexString(signedMessage);
//		return hexValue;
//	}
//
//
//	/**
//	 * ǩ������
//	 */
////	public static String signTransaction4swap(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to,
////										 BigInteger value, String data, byte chainId, String privateKey) throws IOException {
////		byte[] signedMessage;
////		RawTransaction rawTransaction = RawTransaction.createTransaction(
////				nonce,
////				gasPrice,
////				gasLimit,
////				to,
////				value,
////				data);
////
////		if (privateKey.startsWith("0x")) {
////			privateKey = privateKey.substring(2);
////		}
////		ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
////		Credentials credentials = Credentials.create(ecKeyPair);
////
////		if (chainId > ChainId.NONE) {
////			signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
////		} else {
////			signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
////		}
////
////		String hexValue = Numeric.toHexString(signedMessage);
////		return hexValue;
////	}
//
//}
